using DataScripter.Excel.Formulas;

namespace DataScripter.Excel.Table
{
	internal class TableColumnReferenceAbsoluteRow : Formula
	{
		public TableColumn ReferredColumn { get; set; }
		public TableContent ReferredTable { get; set; }
		public int RowNr { get; set; }

		public TableColumnReferenceAbsoluteRow(TableColumn tableColumn, int rowNr)
		{
			ReferredColumn = tableColumn;
			ReferredTable = ReferredColumn.Table;
			RowNr = rowNr;
		}

		public override string Resolve(ICell current)
		{
			int destinationColumnExcelNumbering = ReferredColumn.Index + ReferredTable.StartCell.Column - ReferredTable.NumberOfEmptyColumns;
			int sourceColumnExcelNumbering = current.Column;
			int colDiff = destinationColumnExcelNumbering - sourceColumnExcelNumbering;
			int rowDiff = ReferredTable.StartCell.Row - current.Row + RowNr - 1;
			return current.GetDiff(rowDiff, colDiff).AbsoluteCellReference;
		}
	}
}